#include "alphanumerictable.h"

#include <iterator>
#include <QString>
#include <QDebug>

namespace qrcode{

AlphanumericTable::AlphanumericTable()
{
    ANTable.insert({{'0', 0}, {'1', 1}, {'2', 2}, {'3', 3}, {'4', 4},
                   {'5', 5}, {'6', 6}, {'7', 7}, {'8', 8}, {'9', 9},
                   {'A', 10}, {'B', 11}, {'C', 12}, {'D', 13}, {'E', 14},
                   {'F', 15}, {'G', 16}, {'H', 17}, {'I', 18}, {'J', 19},
                   {'K', 20}, {'L', 21}, {'M', 22}, {'N', 23}, {'O', 24},
                   {'P', 25}, {'Q', 26}, {'R', 27}, {'S', 28}, {'T', 29},
                   {'U', 30}, {'V', 31}, {'W', 32}, {'X', 33}, {'Y', 34},
                   {'Z', 35}, {' ', 36}, {'$', 37}, {'%', 38}, {'*', 39},
                   {'+', 40}, {'-', 41}, {'.', 42}, {'/', 43}, {':', 44}});
}

int AlphanumericTable::GetValueByChar(char &ch)
{
    auto posKey = ANTable.find(ch);
    if(posKey != ANTable.end()){
        return posKey->second;
    } else {
        qDebug() << "alphanumerictable.cpp 27: 没有找到值。";
        return 99;
    }
}

std::__cxx11::string AlphanumericTable::GetTwoResultByValue(int value)
{
    char ch1;
    char ch2;
    bool flag = false;
    std::map<char, int>::iterator pos1;
    std::map<char, int>::iterator pos2;
    for(pos1 = ANTable.begin(); pos1 != ANTable.end(); ++pos1){
        for(pos2 = ANTable.begin(); pos2 != ANTable.end(); ++pos2){
            if((pos1->second*45+pos2->second) == value){
                ch1 = pos1->first;
                ch2 = pos2->first;
                flag = true;
                break;
            }
        }
        if(flag){
            break;
        }
    }
    QString str = QString("%1%2").arg(ch1).arg(ch2);
    return str.toStdString();
}

std::__cxx11::string AlphanumericTable::GetResultByValue(int value)
{
    char ch;
    bool flag = false;
    std::map<char, int>::iterator pos;
    for(pos = ANTable.begin(); pos != ANTable.end(); ++pos){
        if(pos->second == value){
            ch = pos->first;
            flag = true;
            break;
        }
    }
    if(!flag){
        qDebug() << "AlphanumericTable 找不到结果";
    }
    QString str = QString("%1").arg(ch);
    return str.toStdString();
}

}
